layui.use(['form', 'table','layuimini','element'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        layuimini = layui.layuimini;

    /**
     * 初始化表单，要加上，不然刷新部分组件可能会不加载
     */
    form.render();

    var MapLog = {
        id: 'MapLogTable'
    };

    $(function() {
        var index = layer.load(2);

        //初始化省份
        $('select#search_province').append($('<option></option>').attr('selected', 'selected').val('-1').text('全部'));
        var provinceUrl = "/map/publicMapDistrict/provinceList";
        $.get(provinceUrl, {}, function(data){
            layer.close(index);
            if(data.code == "0"){
                var provinceList = data['data'];
                $.each(provinceList, function(index, province) {
                    $('select#search_province').append($('<option></option>').val(province.adcode).text(province.name));
                });
            }else{
                layer.msg(data.msg, {icon:2});
            }
            form.render();
            initCity('-1'); //初始化城市
        });
    });

    /**
     * 监听省份改变事件
     */
    form.on('select(provinceFilter)', function(data){
        initCity(data.value);
    });

    /**
     * 初始化城市
     */
    function initCity(provinceCode){
        $('#search_city').empty();
        $('select#search_city').append($('<option></option>').attr('selected', 'selected').val('-1').text('全部'));
        if(provinceCode === '-1'){
            form.render();
            return false;
        }
        var index = layer.load(2);
        var url = "/map/publicMapDistrict/cityList";
        $.get(url, {provinceCode: provinceCode}, function(data){
            layer.close(index);
            if(data.code == "0"){
                var cityList = data['data'];
                if(cityList.length > 0){
                    $.each(cityList, function(index, city) {
                        $('select#search_city').append($('<option></option>').val(city.adcode).text(city.name));
                    });
                }else{ //没有城市则显示省，例如：[台湾]没有城市、区县、街道，[香港特别行政区]/[澳门特别行政区]没有城市(只有区县)
                    $('select#search_city').append($('<option></option>').val(provinceCode).text($('#search_province').find('option:selected').text()));
                }
            }else{
                layer.msg(data.msg, {icon:2});
            }
            form.render();
        });
    }

    var column = [
        [
            {type: 'numbers', title: '序号'},
            {field: 'id', title: '编号', align: 'center'},
            {field: 'logCode', title: '日志编号', align: 'center'},
            {field: 'platform', title: '平台', align: 'center',
                templet: function (object) {
                    if('gaode' === object['platform']){
                        return '<span class="layui-badge layui-bg-green">高德</span>';
                    }else if('baidu' === object['platform']){
                        return '<span class="layui-badge layui-bg-blue">百度</span>';
                    }
                }
            },
            {field: 'logType', title: '日志类型', align: 'center',
                templet: function (object) {
                    if(1 === object['logType']){
                        return '<span class="layui-badge layui-bg-red">抓取</span>';
                    }else if(2 === object['logType']){
                        return '<span class="layui-badge layui-bg-green">导出</span>';
                    }
                }
            },
            {field: 'province', title: '省份', align: 'center'},
            {field: 'city', title: '城市', align: 'center'},
            {field: 'keyworks', title: '关键词', align: 'center'},
            {field: 'startIndex', title: '数据开始下标', align: 'center'},
            {field: 'endIndex', title: '数据结束下标', align: 'center'},
            {field: 'totalNum', title: '数据总数', align: 'center'},
            {field: 'content', title: '日志内容', align: 'center'},
            {field: 'operateIp', title: '操作IP', align: 'center'},
            {field: 'isShow', title: '是否已回显', align: 'center'},
            {field: 'updateDate', title: '更新时间', minWidth: 120, align: 'center'},
            // {field: 'updateby', title: '更新者', minWidth: 120, align: 'center'},
            // {field: 'createDate', title: '创建时间', minWidth: 120, align: 'center'},
            // {templet: '#stateBtn', width: 140, align: 'center', title: '操作'}
        ]
    ];

    table.render({
        elem: '#' + MapLog.id,
        url: '/map/mapLog/list',
        toolbar: '#toolbarBtn',
        defaultToolbar: ['filter', 'exports', 'print'],
        cols: column,
        limits: [10, 15, 20, 25, 50, 100],
        limit: 15,
        page: true
    });

    // 监听搜索操作
    $("#data-search-btn").on('click', function () {

        //获取搜索条件
        var platform = $("#search_platform").val();
        var logType = $("#search_logType").val();
        var province = $("#search_province").val();
        var city = $("#search_city").val();

        //封装
        var dataObject = {};
        dataObject['platform'] = platform;
        dataObject['logType'] = logType;
        dataObject['province'] = province;
        dataObject['city'] = city;

        var result = JSON.stringify(dataObject);

        //执行搜索重载
        table.reload(MapLog.id, {
            page: {
                curr: 1
            }
            , where: {
                searchParams: result
            }
        }, 'data');

        return false;
    });

    // 监听重置操作
    $("#data-reset-btn").on('click', function () {
        initCity('-1'); //初始化城市
        //初始化
        $("#search_platform").val("-1");
        $("#search_logType").val("-1");
        $("#search_province").val("-1");
        $("#search_city").val("-1");

        form.render();

        //封装
        var dataObject = {};
        dataObject['platform'] = "-1";
        dataObject['logType'] = "-1";
        dataObject['province'] = "-1";
        dataObject['city'] = "-1";
        var result = JSON.stringify(dataObject);

        //执行搜索重载
        table.reload(MapLog.id, {
            page: {
                curr: 1
            }
            , where: {
                searchParams: result
            }
        }, 'data');

        return false;
    });

    /**
     * 点击删除按钮
     * @param id
     */
    MapLog.delete = function(id){
        layer.open({
            title: '删除日志'
            ,content: '是否确定删除该日志？'
            ,icon: 3
            ,btn: ['确定', '取消']
            ,yes: function(index, layero){
                layer.close(index);
                var indexLoad = layer.load(2);
                var url = '/map/mapLog/delete';
                $.post(url, {id: id}, function(data){
                    layer.close(indexLoad);
                    if(data.state == "200"){
                        layer.msg(data.message, {icon: 1});
                        table.reload(MapLog.id);
                    }else{
                        layer.msg(data.message, {icon: 2});
                    }
                })
            }
        });

        return false;
    };


    //监听表格复选框选择
    // table.on('checkbox(currentTableFilter)', function (obj) {
    //     console.log(obj)
    // });

    //监听工具条
    table.on('tool(' + MapLog.id + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'delete') {
            MapLog.delete(data.id);
        }
    });

});